package com.example.springboot.service;

import com.example.springboot.dao.AdminRepository;
import com.example.springboot.entity.Admin;
import com.example.springboot.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class AdminService {
        @Autowired
        private AdminRepository adminRepository;

        public Admin register(Admin admin) {
            return null;
        }

        public Admin login(Admin admin) {
            if (admin.getUsername() == null ||"".equals(admin.getUsername())){
                throw new CustomException("用户名不能为空");
            }
            if (admin.getPassword() == null || "".equals(admin.getPassword())) {
                throw new CustomException("密码不能为空");
            }
            Admin user = adminRepository.findByUsernameAndPassword(admin.getUsername(), admin.getPassword());
            if (user == null) {
                throw new CustomException("用户名或密码错误");
            }

            return user;
        }


}

